package liquidfs.core;

import java.nio.charset.*;

import liquidfs.*;
import liquidfs.data.ops.*;

public class JournalEntry {
   
   private ModificationOperation<?> currentOperation;
   private Object currentOperationState;
   private byte[] data;

   public JournalEntry (ModificationOperation<?> currentOperation, Object currentOperationState) {
      this (currentOperation, currentOperationState, null);
   }
   
   public JournalEntry (ModificationOperation<?> currentOperation, Object currentOperationState, byte[] data) {
      this.currentOperation = currentOperation;
      this.currentOperationState = currentOperationState;
      this.data = data;
   }

   public ModificationOperation<?> getCurrentOperation() {
      return currentOperation;
   }

   public void setCurrentOperation(ModificationOperation<?> currentOperation) {
      this.currentOperation = currentOperation;
   }

   public Object getCurrentOperationState() {
      return currentOperationState;
   }

   public void setCurrentOperationState(Object currentOperationState) {
      this.currentOperationState = currentOperationState;
   }
   
   public byte[] getData() {
      return data;
   }

   public void setData(byte[] data) {
      this.data = data;
   }

   public byte[] serialize (Application app) {
      Serializer serializer = Serializer.getDefaultSerializer(app);
      return serializer.serialize(this).getBytes(Charset.forName("UTF-8"));
   }
   
   public static JournalEntry deserialize (Application app, byte[] bytes) {
      Serializer serializer = Serializer.getDefaultSerializer(app);
      return (JournalEntry) serializer.deserialize(new String(bytes, Charset.forName("UTF-8")));
   }
}
